package com.ruoyi.project.modules.userDataAuth.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.project.modules.userDataAuth.vo.DataAuthUserInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.userDataAuth.domain.UserDataAuth;
import com.ruoyi.project.modules.userDataAuth.service.IUserDataAuthService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户数据权限Controller
 *
 * @author smallrain
 * @date 2021-11-09
 */
@Controller
@RequestMapping("/userDataAuth/userDataAuth")
public class UserDataAuthController extends BaseController {
    private String prefix = "userDataAuth/userDataAuth";

    @Autowired
    private IUserDataAuthService userDataAuthService;

    @RequiresPermissions("userDataAuth:userDataAuth:view")
    @GetMapping()
    public String userDataAuth() {
        return prefix + "/userDataAuth";
    }

    /**
     * 查询用户数据权限列表
     */
    @RequiresPermissions("userDataAuth:userDataAuth:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserDataAuth userDataAuth) {
        startPage();
        List<UserDataAuth> list = userDataAuthService.selectUserDataAuthList(userDataAuth);
        return getDataTable(list);
    }

    @PostMapping("/dataAuthList")
    @ResponseBody
    public TableDataInfo dataAuthList(UserDataAuth userDataAuth) {
        startPage();
        Long roleId = userDataAuth.getRoleId();
        String objectType = "";
        if (roleId == 100) {
            objectType = "1";
        } else if (roleId == 101) {
            objectType = "2";
        } else if (roleId == 102) {
            objectType = "3";
        } else if (roleId == 103) {
            objectType = "4";
        } else if (roleId == 104) {
            objectType = "5";
        } else {
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(0);
            rspData.setRows(new ArrayList<>());
            rspData.setTotal(0);
            return rspData;
        }
        userDataAuth.setObjectType(objectType);
        List<UserDataAuth> list = userDataAuthService.selectUserDataAuthList2(userDataAuth);
        return getDataTable(list);
    }


    @GetMapping("/selectObject")
    public String selectObject(@RequestParam Long userId, @RequestParam Long roleId, ModelMap mmap) {
        mmap.put("userId", userId);
        mmap.put("roleId", roleId);
        return prefix + "/selectObject";
    }

    @PostMapping("/unSelectObjectList")
    @ResponseBody
    public TableDataInfo unSelectObjectList(UserDataAuth userDataAuth) {
        startPage();
        Long roleId = userDataAuth.getRoleId();
        String objectType = "";
        if (roleId == 100) {
            objectType = "1";
        } else if (roleId == 101) {
            objectType = "2";
        } else if (roleId == 102) {
            objectType = "3";
        } else if (roleId == 103) {
            objectType = "4";
        } else if (roleId == 104) {
            objectType = "5";
        } else {
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(0);
            rspData.setRows(new ArrayList<>());
            rspData.setTotal(0);
            return rspData;
        }
        userDataAuth.setObjectType(objectType);
        List<UserDataAuth> list = userDataAuthService.unSelectObjectList(userDataAuth);
        return getDataTable(list);
    }

    /**
     * 导出用户数据权限列表
     */
    @RequiresPermissions("userDataAuth:userDataAuth:export")
    @Log(title = "用户数据权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserDataAuth userDataAuth) {
        List<UserDataAuth> list = userDataAuthService.selectUserDataAuthList(userDataAuth);
        ExcelUtil<UserDataAuth> util = new ExcelUtil<UserDataAuth>(UserDataAuth.class);
        return util.exportExcel(list, "用户数据权限数据");
    }

    /**
     * 新增用户数据权限
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存用户数据权限
     */
    @RequiresPermissions("userDataAuth:userDataAuth:add")
    @Log(title = "用户数据权限", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserDataAuth userDataAuth) {
        return toAjax(userDataAuthService.insertUserDataAuth(userDataAuth));
    }

    /**
     * 修改用户数据权限
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        UserDataAuth userDataAuth = userDataAuthService.selectUserDataAuthById(id);
        mmap.put("userDataAuth", userDataAuth);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户数据权限
     */
    //@RequiresPermissions("userDataAuth:userDataAuth:edit")
    @Log(title = "用户数据权限", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserDataAuth userDataAuth) {
        return toAjax(userDataAuthService.updateUserDataAuth(userDataAuth));
    }

    /**
     * 删除用户数据权限
     */
    @RequiresPermissions("userDataAuth:userDataAuth:remove")
    @Log(title = "用户数据权限", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(userDataAuthService.deleteUserDataAuthByIds(ids));
    }

    @PostMapping("/selectAll")
    @ResponseBody
    public AjaxResult selectAuthUserAll(Long roleId, Long userId, String objectIds) {
        String objectType = "";
        if (roleId == 100) {
            objectType = "1";
        } else if (roleId == 101) {
            objectType = "2";
        } else if (roleId == 102) {
            objectType = "3";
        } else if (roleId == 103) {
            objectType = "4";
        } else if (roleId == 104) {
            objectType = "5";
        } else {
            return this.error("参数错误");
        }
        return toAjax(userDataAuthService.insertDataAuthUsers(objectType, userId, objectIds));
    }

    @PostMapping("/cancel")
    @ResponseBody
    public AjaxResult cancelAll(String ids) {
        return toAjax(userDataAuthService.cancelAll(ids));
    }

    @PostMapping("/userList")
    @ResponseBody
    public AjaxResult userList(@RequestParam() String objectType) {
        List<DataAuthUserInfo> dataAuthUserList = this.userDataAuthService.selectDataAuthUserListByObjectType(objectType);
        return AjaxResult.success(dataAuthUserList);
    }

    @PostMapping("/userAndDataSummaryList")
    @ResponseBody
    public AjaxResult userAndDataSummaryList(@RequestParam() String objectType) {
        List<DataAuthUserInfo> result = this.userDataAuthService.selectUserAndDataSummaryList(objectType);
        return AjaxResult.success(result);
    }

    @PostMapping("/dataList")
    @ResponseBody
    public AjaxResult dataList(UserDataAuth userDataAuth) {
        List<UserDataAuth> result = this.userDataAuthService.selectDataListByParams(userDataAuth);
        return AjaxResult.success(result);
    }

    /**
     * 更新数据权限状态
     * objectType=1&objectId=1&objectStatus=0
     * @author chenxiaoyu
     * @date 2022/3/22 下午10:57
     * @param userDataAuth
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @PostMapping("/updateStatus")
    @ResponseBody
    public AjaxResult updateStatus(UserDataAuth userDataAuth) {
        userDataAuth.setUserId(ShiroUtils.getUserId());
        this.userDataAuthService.updateStatus(userDataAuth);
        return AjaxResult.success();
    }
}
